package com.wushijia.config;

import com.alibaba.druid.pool.DruidDataSource;
import java.sql.SQLException;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "customerEntityManager", transactionManagerRef = "customerTransactionManager",
    basePackages = {"com.wushijia"})
public class PrimaryDBConfig {

  @Autowired
  DataBaseConnection dataBaseConnection;

  @Bean(name = "customerEntityManager")
  public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan(dataBaseConnection.getPackagesToScan());
    em.setJpaVendorAdapter(vendorAdapter);
    em.setJpaProperties(dataBaseConnection.getHibernateProperties());
    em.setPersistenceUnitName("customers");
    return em;
  }

  @Bean(name = "customerTransactionManager")
  @Primary
  public JpaTransactionManager transactionManager(EntityManagerFactory customerEntityManager){
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(customerEntityManager);
    return transactionManager;
  }

  @Bean(name = "druidDataSource")
  public DataSource dataSource() {
    DruidDataSource dds = new DruidDataSource();
    dds.setDriverClassName(dataBaseConnection.getDriverClassName());
    dds.setUrl(dataBaseConnection.getUrl());
    dds.setUsername(dataBaseConnection.getUsername());
    dds.setPassword(dataBaseConnection.getPassword());

    //配置初始化大小、最小、最大
    dds.setMaxActive(20);
    dds.setInitialSize(5);
    dds.setMinIdle(5);

    //配置获取连接等待超时的时间  单位毫秒
    dds.setMaxWait(20000);
    //配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
    dds.setTimeBetweenEvictionRunsMillis(60000);
    //配置一个连接在池中最小生存的时间，单位是毫秒
    dds.setMinEvictableIdleTimeMillis(300000);

    dds.setValidationQuery("SELECT 'x'");
    dds.setTestWhileIdle(true);
    dds.setTestOnReturn(false);
    dds.setTestOnBorrow(false);

    //打开PSCache，并且指定每个连接上PSCache的大小
    dds.setPoolPreparedStatements(false);
    //dds.setMaxPoolPreparedStatementPerConnectionSize(20);

    //配置监控统计拦截的filters
    try {
      dds.setFilters("stat");
    } catch (SQLException e) {
      e.printStackTrace();
    }

    //打开removeAbandoned功能
    dds.setRemoveAbandoned(true);
    // 1800秒，也就是30分钟
    dds.setRemoveAbandonedTimeout(1800);
    dds.setLogAbandoned(true);
    return dds;
  }

}
